import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class update {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入修改对象的名字:");
        String name = scanner.next();
        System.out.println("输入修改后的年龄");
        int age = scanner.nextInt();

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/box?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("0327");

        Connection connection = dataSource.getConnection();

        String sql = "update student set age = ? where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(2,name);
        statement.setInt(1,age);
        System.out.println(statement);


        int lines = statement.executeUpdate();
        System.out.println(lines);

        statement.close();
        connection.close();
    }
}
